#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <QMutex>
#include <QMutexLocker>

extern QMutex mutex;

#define GEC6818_GET_DHTDATA  _IOR('K', 0, unsigned int) //注册宏

static int ultrasonic_fd = -1;

int init_dht11(void)
{
	ultrasonic_fd = open("/dev/dht11_dev", O_RDWR);
	if(ultrasonic_fd < 0)
	{
		perror("open dht11_dev driver");
		return -1;
	}	

	//close(ultrasonic_fd);
    printf("open dht11_dev ok\n");
	return 0;
}

int read_ht(int &h, int &t)
{

	int ret = 0;
	unsigned char data[2];
    
    if(ultrasonic_fd >= 0)
    {
        ret = ioctl(ultrasonic_fd, GEC6818_GET_DHTDATA, &data[0]);
        if(ret != 0)
        {
            perror("GEC6818_GET_DHTDATA error");
        }
        else
        {
            printf("h= %d  t=%d \n", data[1], data[0]);
            h = data[1];
            t = data[0];
            ret = 1;
        }
    }

	return ret;
}
